import json
import time


# import pickle

class CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, set):
            return {'__set__': list(obj)}
        elif isinstance(obj, tuple):
            return {'__tuple__': list(obj)}
        return super().default(obj)


test_data = {
    'integer': 42,
    'float': 3.14159,
    'string': 'Hello, World!',
    'boolean': True,
    'none_value': None,
    'list': [1, 2, 3, 'four', 5.0],
    'tuple': (1, 2, 3),
    'nested_dict': {
        'key1': 'value1',
        'key2': [10, 20, 30]
    },
    'set': {1, 2, 3}  # set类型需要特殊注意
}

print("原始数据:")
for key, value in test_data.items():
    print(f"  {key}: {value} ({type(value).__name__})")

# 序列化
serialized_data = json.dumps(test_data,cls=CustomEncoder)
print(f"\n序列化后的数据长度: {len(serialized_data)} 字节")

# 反序列化
# deserialized_data = json.loads(serialized_data)
# print("\n反序列化后的数据:")
# for key, value in deserialized_data.items():
#     print(f"  {key}: {value} ({type(value).__name__})")
#
# # 验证数据一致性
# print(f"\n数据一致性检查: {test_data == deserialized_data}")


